<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="新闻类别名称" prop="newsTypeName">
        <el-input v-model="queryParams.newsTypeName" placeholder="请输入新闻类别名称" clearable size="small"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="新闻类别状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择新闻类别状态" clearable size="small">
          <el-option v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel"
                     :value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['news:type:add']">
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                   v-hasPermi="['news:type:edit']">
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                   v-hasPermi="['news:type:remove']">
          删除
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="typeList" @selection-change="handleSelectionChange" size="mini" :height="tableHeight">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="新闻类别编号" align="center" prop="newsTypeId"/>
      <el-table-column label="类别名称" align="center" prop="newsTypeName">
        <template slot-scope="scope">
          <span style="font-weight: bolder">{{ scope.row.newsTypeName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类别状态" align="center" prop="status" :formatter="statusFormat"/>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['news:type:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['news:type:remove']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="pageNum" :limit.sync="pageSize" @pagination="getList"/>

    <!-- 添加或修改新闻类型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="新闻类别名称" prop="newsTypeName">
          <el-input v-model="form.newsTypeName" placeholder="请输入新闻类别名称"/>
        </el-form-item>
        <el-form-item label="新闻类别状态">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictValue">{{ dict.dictLabel }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listType, getType, delType, addType, updateType } from '@/api/news/type'

export default {
  name: 'Type',
  data () {
    return {
      tableHeight: window.innerHeight - 340,
      // 日期范围
      dateRange: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      pageNum: 1,
      pageSize: 10,
      // 总条数
      total: 0,
      // 新闻类型表格数据
      typeList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 新闻类别状态字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        newsTypeName: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        newsTypeName: [
          { required: true, message: '新闻类别名称不能为空', trigger: 'blur' }
        ],
      }
    }
  },
  mounted () {
    //后面的50：根据需求空出的高度，自行调整
    this.$nextTick(() => {
      this.tableHeight = window.innerHeight - 340
    })
  },
  created () {
    this.getList()
    this.getDicts('sys_normal_disable').then(response => {
      this.statusOptions = response.data
    })
  },
  methods: {
    /** 查询新闻类型列表 */
    getList () {
      this.loading = true
      listType(this.pageNum, this.pageSize, this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.typeList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 新闻类别状态字典翻译
    statusFormat (row, column) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.form = {
        newsTypeId: undefined,
        newsTypeName: undefined,
        status: '0',
        remark: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.newsTypeId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset()
      this.open = true
      this.title = '添加新闻类型'
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset()
      const newsTypeId = row.newsTypeId || this.ids
      getType(newsTypeId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改新闻类型'
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.newsTypeId !== undefined) {
            updateType(this.form).then(response => {
              if (response.code === 200) {
                this.msgNoticeSuccess('修改成功')
                this.open = false
                this.getList()
              }
            })
          } else {
            addType(this.form).then(response => {
              if (response.code === 200) {
                this.msgNoticeSuccess('新增成功')
                this.open = false
                this.getList()
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const newsTypeIds = row.newsTypeId || this.ids
      this.$confirm('是否确认删除新闻类型编号为"' + newsTypeIds + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delType(newsTypeIds)
      }).then(() => {
        this.getList()
        this.msgNoticeSuccess('删除成功')
      }).catch(function () {
      })
    },
  }
}
</script>
